import { defineConfig } from "vite"
import vue from "@vitejs/plugin-vue"
import { resolve } from "path"
import styleImport, { VantResolve } from 'vite-plugin-style-import';
import resolveExternalsPlugin from 'vite-plugin-resolve-externals'
import copy from 'rollup-plugin-copy' //引入插件

export default defineConfig({
  css: {
    //css预处理
    preprocessorOptions: {
      scss: {
        charset: false,
        //引入var.scss全局预定义变量
        additionalData: '@import "./src/assets/css/reset.scss";'
      },
    }
  },
  plugins: [
    vue(),
    styleImport({
      resolves: [VantResolve()],
    }),
    resolveExternalsPlugin({
      BMap: 'BMap' // 这个名字可以直接打印window，看window上挂的是什么名字，就写什么名字
    }),
    copy({
      targets: [
        { src: './public/index.html', dest: './Index' }, //执行拷贝
      ]
    })
  ],
  base: "./", // 类似publicPath，'./'避免打包访问后空白页面，要加上，不然线上也访问不了
  resolve: {
    alias: {
      // 如果报错__dirname找不到，需要安装node,执行npm install @types/node --save-dev
      "@": resolve(__dirname, "src"),
      "@assets": resolve(__dirname, "src/assets"),
      "@components": resolve(__dirname, "src/components"),
      "@images": resolve(__dirname, "src/assets/images"),
      "@views": resolve(__dirname, "src/views"),
      "@store": resolve(__dirname, "src/store"),
    },
  },
  build: {
    target: "modules",
    outDir: "dist",
    assetsDir: "static", //指定静态资源存放路径
    sourcemap: false, //是否构建source map 文件
    minify: "terser",
    manifest: true,
    emptyOutDir: true,
    terserOptions: {
      // 生产环境移除console
      compress: {
        drop_console: true,
        drop_debugger: true,
      },
    },
    rollupOptions: {
      output: {
        chunkFileNames: "static/js/[name]-[hash].js",
        entryFileNames: "static/js/[name]-[hash].js",
        // assetFileNames: "static/[ext]/[name]-[hash].[ext]" // 背景图片引用有问题
      }
    }
  },
  server: {
    https: false, // 是否开启 https
    open: false, // 是否自动在浏览器打开
    port: 8001, // 端口号
    host: "0.0.0.0",
    proxy: {
      "/api": {
        target: "", // 后台接口
        changeOrigin: true,
        secure: false, // 如果是https接口，需要配置这个参数
        // ws: true, //websocket支持
        rewrite: (path) => path.replace(/^\/api/, ""),
      },
    },
  },
  // 引入第三方的配置
  optimizeDeps: {
    include: [],
  },
});